package fun.coding.leetcode;

public class RemoveDuplicatesFromSortedArray {

	public static void main(String[] args) {
		RemoveDuplicatesFromSortedArray ins = new RemoveDuplicatesFromSortedArray();
		int[] test1 = {1, 2, 3};
		System.out.println(ins.removeDuplicates(test1));
		
		int[] test2 = {1, 1, 1, 2, 2, 2, 3, 3, 4};
		System.out.println(ins.removeDuplicates(test2));
	}
	
	public int removeDuplicates(int[] A) {
		if (A == null || A.length == 0) return 0;
		
		if (A.length == 1) return 1;
		
		int pre = 0;
		int cur = pre + 1;
		while (cur < A.length) {
			if (A[cur] != A[pre]) {
				A[pre + 1] = A[cur];
				pre++;
			}
			cur++;
		}
		
		return pre + 1;
	}
	
	/* a more neat solution
    public int removeDuplicates(int[] A) {
        if (A == null || A.length == 0) {
            return 0;
        }
        
        int size = 0;
        for (int i = 0; i < A.length; i++) {
            if (A[i] != A[size]) {
                A[++size] = A[i];
            }
        }
        return size + 1;
    }
	*/
}
